/*
 * @(#)MessageMethod.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.macro.method.component;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.nhncorp.lucy.common.message.MessageUtil;
import com.nhncorp.lucy.common.util.LocaleUtil;

import com.nhncorp.usf.core.util.StringUtil;

import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;

/**
 * @author Web Platform Development Team
 */
public class MessageMethod implements TemplateMethodModel {
    Log log = LogFactory.getLog(MessageMethod.class);

    /**
     * @param args the arguments
     * @return the message
     * @throws TemplateModelException the TemplateModelException
     */
    @SuppressWarnings("unchecked")
	public Object exec(List args) throws TemplateModelException {
        if (args.size() < 1) {
            throw new IllegalArgumentException("Arguments size must be lager than 1.");
        }

        Locale locale = null;
        String key = (String)args.get(0);

        if (StringUtil.isEmpty(key)) {
            throw new IllegalArgumentException("Key value must not be empty.");
        }

        log.debug("message key = " + key);
        String defaultMessage = (String)args.get(1);

        if (StringUtil.isEmpty(defaultMessage)) {
            defaultMessage = null;
        }

        log.debug("defaultMessage = " + defaultMessage);
        String language = (String)args.get(2);

        if (StringUtil.isNotEmpty(language)) {
            locale = new Locale(language);
        }

        log.debug("locale = " + locale);
        List arguments = addArguments(args);

        // check local
        locale = (locale == null) ? LocaleUtil.getUserLocale() : locale;

        // Retrieve the message string we are looking for
        String message = MessageUtil.getMessage(locale, key, arguments.toArray());

        if (message == null) {

            if (defaultMessage == null) {
                log.warn("message about key(" + key + ") doesn't exist.");
                message = "unknown";
            } else {
                message = defaultMessage;
            }

        }

        return message;
    }

    /**
     * Adds the arguments.
     *
     * @param args the args
     * @return the list
     */
    @SuppressWarnings("unchecked")
	private List addArguments(List args) {
        List<String> arguments = new ArrayList<String>();
        String arg0 = (String)args.get(3);
        log.debug("arg0 = " + arg0);

        String arg1 = (String)args.get(4);
        log.debug("arg1 = " + arg1);

        String arg2 = (String)args.get(5);
        log.debug("arg2 = " + arg2);

        String arg3 = (String)args.get(6);
        log.debug("arg3 = " + arg3);

        String arg4 = (String)args.get(7);
        log.debug("arg4 = " + arg4);

        if (StringUtil.isNotEmpty(arg4)) {
            arguments.add(0, arg4);
        }

        if (StringUtil.isNotEmpty(arg3)) {
            arguments.add(0, arg3);
        }

        if (StringUtil.isNotEmpty(arg2)) {
            arguments.add(0, arg2);
        }

        if (StringUtil.isNotEmpty(arg1)) {
            arguments.add(0, arg1);
        }

        if (StringUtil.isNotEmpty(arg0)) {
            arguments.add(0, arg0);
        }

        return arguments;
    }
}
